package com.lunago.agent.service.impl;

import com.lunago.agent.constant.ServiceCode;
import com.lunago.agent.ex.ServiceException;
import com.lunago.agent.mapper.UserGemsLogMapper;
import com.lunago.agent.mapper.UserGemsMapper;
import com.lunago.agent.pojo.entity.UserGems;
import com.lunago.agent.pojo.entity.UserGemsExample;
import com.lunago.agent.pojo.entity.UserGemsLog;
import com.lunago.agent.pojo.entity.UserGemsLogExample;
import com.lunago.agent.service.UserGemsService;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserGemsServiceImpl implements UserGemsService {

    @Autowired
    private UserGemsMapper userGemsMapper;
    @Autowired
    private UserGemsLogMapper userGemsLogMapper;;

    @Override
    public void consumeGems(Long userId, int gems) {
        UserGems dbUserGems = queryMyGems(userId);
        if (dbUserGems.getGems() >= gems) {  // 足够扣
            // 计算剩余gems
            int remainingGems = dbUserGems.getGems() - gems < 0 ? 0 : dbUserGems.getGems() - gems;
            dbUserGems.setGems(remainingGems);
            // 修改数据库
            userGemsMapper.updateByPrimaryKeySelective(dbUserGems);
        }else { // 不够扣
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, "用户Gems不足");
        }
    }

    @Override
    public UserGems queryMyGems(Long userId) {
        UserGemsExample example = new UserGemsExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<UserGems> list = userGemsMapper.selectByExample(example);
        if (list == null || list.size() == 0) { // # 没有初始化数据，初始化信息
            UserGems userGems = new UserGems();
            userGems.setUserId(userId);
            userGems.setGems(10);
            userGemsMapper.insert(userGems);
            return userGems;
        }
        return list.get(0);
    }

    @Override
    public int share(Long userId) {
        UserGems userGems = queryMyGems(userId);
        // 查询当日分享次数
        int count = queryShareTimesToday(userId);
        if (count < 3) {
            // 执行分享业务，添加日志
            UserGemsLog userGemsLog = new UserGemsLog();
            userGemsLog.setUserId(userId);
            userGemsLog.setCreateTime(new Date());
            userGemsLog.setRemark("分享获取Gems");
            userGemsLog.setGems(userGems.getGems() + 1);
            userGemsLogMapper.insert(userGemsLog);
            // 增加用户积分
            userGems.setGems(userGems.getGems() + 1);
            userGemsMapper.updateByPrimaryKeySelective(userGems);
            return count + 1;
        }else {
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, "当日分享次数已达上限");
        }
    }

    @Override
    public int queryShareTimesToday(Long userId) {
        UserGemsLogExample example = new UserGemsLogExample();
        Date today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);
        example.createCriteria().andUserIdEqualTo(userId).andCreateTimeGreaterThanOrEqualTo(today);
        int count = userGemsLogMapper.countByExample(example);
        return count;
    }

    @Override
    public void updateUserGems(UserGems userGems) {
        userGemsMapper.updateByPrimaryKeySelective(userGems);
    }
}
